package group;

public class Soution {
    /*
    给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外，你可以假设该网格的四条边均被水包围。
示例 1：
输入：grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出：1
示例 2：
输入：grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出：3
     */
    public int numIslands(char[][] grid) {
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    res += 1;
                    bfs(grid, i, j);
                }
            }
        }
        return res;
    }

    public void bfs(char[][] grid, int i, int j) {
        if (i >= grid.length || j >= grid[0].length || i < 0 || j < 0) {
            return;
        }
        if (grid[i][j] == '0' || grid[i][j] == '2') {
            return;
        }
        grid[i][j] = '2';
        bfs(grid, i + 1, j);
        bfs(grid, i - 1, j);
        bfs(grid, i, j + 1);
        bfs(grid, i, j - 1);
    }

    /*
    在给定的 m x n 网格 grid 中，每个单元格可以有以下三个值之一：
值 0 代表空单元格；
值 1 代表新鲜橘子；
值 2 代表腐烂的橘子。
每分钟，腐烂的橘子 周围 4 个方向上相邻 的新鲜橘子都会腐烂。
返回 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能，返回 -1 。
示例 1：
输入：grid = [[2,1,1],[1,1,0],[0,1,1]]
输出：4
示例 2：
输入：grid = [[2,1,1],[0,1,1],[1,0,1]]
输出：-1
解释：左下角的橘子（第 2 行， 第 0 列）永远不会腐烂，因为腐烂只会发生在 4 个方向上。
示例 3：
输入：grid = [[0,2]]
输出：0
解释：因为 0 分钟时已经没有新鲜橘子了，所以答案就是 0 。
     */
    int orange_rotting=0;
    public int orangesRotting(int[][] grid) {
        int minutes = 0;
        int orange=0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                    if(grid[i][j]==1)
                        orange++;
            }
        }
        while(bfs(grid)!=0){
            minutes++;
        }
        return orange_rotting==orange?minutes:-1;
    }
    public int bfs(int[][] grid){
       int count = 0;
       for(int i = 0;i< grid.length;i++) {
           for(int j=0;j<grid[0].length;j++) {
               if (grid[i][j] == 2) {
                   if (i - 1 >= 0 && grid[i - 1][j] == 1) {
                       grid[i - 1][j] = 3;
                       count++;
                   }
                   if (i + 1 < grid.length && grid[i + 1][j] == 1) {
                       grid[i + 1][j] = 3;
                       count++;
                   }
                   if (j - 1 >= 0 && grid[i][j - 1] == 1) {
                       grid[i][j - 1] = 3;
                       count++;
                   }
                   if (j + 1 < grid[0].length && grid[i][j + 1] == 1) {
                       grid[i][j + 1] = 3;
                       count++;
                   }
               }
           }
       }
        orange_rotting+=count;
        for(int i=0;i< grid.length;i++){
            for(int j=0;j<grid[0].length;i++){
                if(grid[i][j]==3){
                    grid[i][j]=2;
                }
            }
        }
        return count;
    }
}
class Test{
    public static void main(String[] args) {
        int[][] grid ={
                {2,0,1,1,1,1,1,1,1,1},
                {1,0,1,0,0,0,0,0,0,1},
                {1,0,1,0,1,1,1,1,0,1},
                {1,0,1,0,1,0,0,1,0,1},
                {1,0,1,0,1,0,0,1,0,1},
                {1,0,1,0,1,1,0,1,0,1},
                {1,0,1,0,0,0,0,1,0,1},
                {1,0,1,1,1,1,1,1,0,1},
                {1,0,0,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,1,1,1,1}
};
        Soution soution = new Soution();
        int i = soution.orangesRotting(grid);
    }
}
